/**
 * problem_022_stl.cpp
 * Copyright (C) 2011 - xrose
 */

#include <iostream>
#include <string.h>
#include <map>
#include <list>
#include <stdio.h>
using namespace std;

class P_NUM
{
    private:
        int perfect;
        int sumDivisor();   //sum of all divisors of and less P_NUM

    public:
        P_NUM();
        P_NUM(int);
        int isPerfect();
        int isPerfect(int);
        int Perfect(int);
//--
        int sumDivisor(int x_perfect);
        int isDeficient();
        int isDeficient(int);
        int isAbundant();
        int isAbundant(int);
//--
        int isDivable(int);
};

P_NUM::P_NUM()
{
    perfect = 0;
}
P_NUM::P_NUM(int x)
{
    perfect = x;
}
int P_NUM::sumDivisor()
{
    int i = 0;
    int sum = 1;
    if(perfect<2) return 0;
    for(i=2; i<=(perfect/2.0); i++)
    {
        if(perfect%i == 0)
        {
            sum+=i;
        }
    }
    return sum;
}
int P_NUM::sumDivisor(int x_perfect)
{
    int i;
    int sum = 1;
    if(x_perfect<=2) return 0;
    for(i=2; i<=(x_perfect/2.0); i++)
    {
        if(x_perfect%i == 0)
        {
            sum+=i;
        }
    }
    return sum;
}
int P_NUM::isPerfect()
{
    if(sumDivisor() == perfect)
    {
        return 1;
    } else
    {
        cout<<"__This is not perfect number"<<endl;
        return 0;
    }
}
int P_NUM::isPerfect(int x)
{
    if(sumDivisor(x) == x)
    {
        cout<<x<<" is a perfect number"<<endl;
        return 1;
    } else
    {
        cout<<x<<" is not a perfect number"<<endl;
        return 0;
    }
}

int P_NUM::Perfect(int xth)
{
    int _numb = 2;
    int itor = 0;
    while(itor < xth)
    {
        if(isPerfect(_numb))
        {
            itor ++;
        }
        _numb++;
    }
    return _numb-1;
}
int P_NUM::isAbundant()
{
    if(sumDivisor() > perfect)
    {
        return 1;
    } else
    {
        cout<<"__This is not abundant number"<<endl;
        return 0;
    }
}
int P_NUM::isAbundant(int x)
{
//    cout <<"sumDivisor()"<<sumDivisor(x)<<endl;
    if(sumDivisor(x) > x)
    {
//        cout<<x<<" is a abundant number"<<endl;
        return 1;
    } else
    {
//        cout<<x<<" is not a abundant number"<<endl;
        return 0;
    }
}
int P_NUM::isDivable(int x)
{
    int i = 1;
    int x_2 = x/2.0;
    while(i<=x_2)
    {
        if (isAbundant(i)&&isAbundant(x-i))
        {
            return 1;
        }
        i++;
    }
    return 0;
}

int main (int argc, char const* argv[])
{
//    P_NUM p_x(1);
//    p_x.isPerfect();
//    p_x.isPerfect(6);
    P_NUM px2;
//    px2.isPerfect(8128);
//    int n =5;
//    int p = px2.Perfect(n);
//    cout<<"Perfect number "<<n<<": "<<p<<endl;
//    int i = 1;
//    cout<<"px2.Divable(i)"<<px2.isDivable(i);
//        int sum = 0;
//        while(i<21500)
//        {
//            if(!px2.isDivable(i))
//            {
//                cout<<"number "<<i<<"cant divide into two abundant number"<<endl;
//                sum +=i;
//            }
//    //        getchar();
//            i++;
//            cout<<"__"<<i<<endl;
//        }
//        cout <<"sum = "<<sum<<endl;
//    cout<<"sumDivisor()"<<px2.sumDivisor(i)<<endl;
//    cout<<"isDivable()"<<px2.isDivable(23)<<endl;
    int i = 0;
    list<int> l_Abundant;
    while(i<=28123)
    {
        if(px2.isAbundant(i))
        {
//            cout<<"Abundant: "<<i<<endl;
//            getchar();
            l_Abundant.push_back(i);
        }
        i++;
    }
    list<int>::iterator itor1, itor2;
    int iAbun;
    bool sum_abundant[28123*2+1] = {false};

    for(itor1=l_Abundant.begin(); itor1!=l_Abundant.end(); ++itor1)
    {
        for(itor2=l_Abundant.begin(); ((itor2--)!=(itor1));++itor2)
        {
            itor2++;
//            cout<<"itor1: "<<*itor1<<" itor2: "<<*itor2<<endl;
//            getchar();
            iAbun = *itor1 + *itor2;
//            cout << "HERE: " << iAbun << endl;
//            if(px2.isAbundant(iAbun))
                sum_abundant[iAbun] = true;
        }

    }

    iAbun = 0;
    for(i=0; i<=28123; i++)
    {
        if(!sum_abundant[i])
        {
            cout<<"i = "<<i<<endl;
//            getchar();
            iAbun+=i;
        }
    }
    cout<<"Answer is: "<<iAbun<<endl;
    return 0;
}
